package com.daiyang.lottery.service.impl;

import com.daiyang.lottery.common.errorcode.ServiceErrorCodeConstants;
import com.daiyang.lottery.common.exception.ServiceException;

import com.daiyang.lottery.controller.param.CreatePrizeParam;
import com.daiyang.lottery.service.PictureService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class PictureServiceImpl implements PictureService {


    @Value("${pic.local-path}")
    private String picturePath;
    @Override
    public String savePicture(MultipartFile pic) {
        File dir =new File(picturePath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //获取文件名
        String fileName =pic.getOriginalFilename();
        //保存文件
        System.out.println("未改的文件名：" + fileName);
        assert fileName!=null;
        //获取文件后缀名
        String suffixName =fileName.substring(fileName.lastIndexOf("."));
        //生成新的文件名
        fileName = UUID.randomUUID() + suffixName;
        System.out.println("picturePath" + picturePath);
        System.out.println("新文件名：" + fileName);

        try {
            pic.transferTo(new File(picturePath+"/"+ fileName));
        } catch (IOException e) {
            throw new ServiceException(ServiceErrorCodeConstants.UPLOAD_PIC_ERROR);
        }
        return fileName;
    }

}
